import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import 'package:study_online/entity/Student.dart';
import 'package:study_online/global/global.dart';

import '../../main.dart';
import 'MyModel.dart';

class MyViewModel extends ChangeNotifier{
  File _image;
  String _imagePath=Global.getInstance().user.photo,_birthDay="待完善",_qq="待完善",_weChat="待完善";
  Student _student=Global.getInstance().user;
  File get image{
    return _image;
  }

  Student get student{
    return this._student;
  }

  String get birthDay{
    return this._birthDay;
  }

  String get qq{
    return this._qq;
  }

  String get weChat{
    return this._weChat;
  }

  void setImg(File image){
    _image=image;
    this.uploadImg();
    notifyListeners();
  }

  void setName(String val){
    _student.setName(val);
    notifyListeners();
  }

  void setMagor(String val){
    _student.setMajor(val);
    notifyListeners();
  }

  void setPhone(String val){
    _student.setPhone(val);
    notifyListeners();
  }

  void setQQ(String val){
    this._qq=val;
    notifyListeners();
  }

  void setWeChat(String val){
    this._weChat=val;
    notifyListeners();
  }

  void setBirthDay(String val){
    this._birthDay=val;
    notifyListeners();
  }

  void uploadImg() async{
    String path = this._image.path;
    String name = path.substring(path.lastIndexOf("/") + 1);
    Response response=await imgUpload(path,name);
    this._imagePath=response.data;
  }

  void updateUser()async{
     Response result=await updateUserInfo(_student.stuId,_student.college,_student.major,_student.name,_student.phone,_imagePath,_qq,_weChat,_student.sex);
     if (result.data["code"] == 0) {
       EasyLoading.showSuccess(result.data["msg"]);
       Navigator.of(navigatorKey.currentContext).pop();
     } else
       EasyLoading.showError(result.data["msg"]);
  }
}